package com.jxmcloud.business.cut.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class SerialNumberUtil {
    /*private static final String SERIAL = "ACTIVITY_PROJECT";

    @Autowired
    private RedissonClient redissonClient;

    public String create(String pre) {
        RLock rLock = redissonClient.getLock(SERIAL);
        try {
            if(rLock.tryLock(3, TimeUnit.SECONDS)){
                try {
                    String key = SERIAL + DateUtil.format(new Date(),"yyyy-MM-dd");
                    RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
                    Long value = atomicLong.get();
                    String no = "";
                    if (ObjectUtil.isEmpty(value)) {
                        no = autoGenericCode(0, 4);
                    } else {
                        value = atomicLong.incrementAndGet();
                        no = autoGenericCode(value, 4);
                    }
                    return pre + no;
                } catch (Exception e){
                    log.error("生成自增编号异常！" , e);
                    throw new BusinessException("生成自增编号异常！");
                }finally {
                    rLock.unlock();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }*/

    public static String autoOrderNo() {
        String payOrderNo = System.currentTimeMillis() + autoGenericCode("" + new Random().nextInt(1000000), 6);
        return payOrderNo;
    }

    public static String autoGenericCode(String code, int num) {
        return String.format("%0" + num + "d", Integer.parseInt(code));
    }

    public static void main(String[] args) {
        /*for(int i =0;i<100;i++) {
            String payOrderNo = System.currentTimeMillis() + autoGenericCode("" + new Random().nextInt(1000000), 6);
            System.out.println(payOrderNo);
        }*/

    }
}
